package org.jeecg.modules.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class JaccardSimilarity {
    public static double jaccardSimilarity(String text1, String text2) {
        Set<String> words1 = new HashSet<>(Arrays.asList(text1.split("\\s+")));
        Set<String> words2 = new HashSet<>(Arrays.asList(text2.split("\\s+")));

        Set<String> intersection = new HashSet<>(words1);
        intersection.retainAll(words2);

        Set<String> union = new HashSet<>(words1);
        union.addAll(words2);

        return intersection.size() / (double) union.size();
    }

    public static void main(String[] args) {
        String text1 = "苹果 香蕉 橙子";
        String text2 = "香蕉 橙子 葡萄";
        double similarity = jaccardSimilarity(text1, text2);
        System.out.println("Jaccard相似度: " + similarity);
    }
}
